package net.contrend.material.dao;

import java.sql.Types;
import java.util.List;
import java.util.UUID;

import net.contrend.material.form.MaterialOutboundSheetForm;
import net.contrend.material.mapper.MaterialOutboundSheetMapper;
import net.contrend.system.dao.BaseDao;
import net.contrend.util.SqlUtils;
import net.contrend.util.StringUtils;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * <p>Copyright @2009 www.contrend.net</p>
 * <p>All right reserved. </p>
 * <p>项目名称			： 进销存管理系统 </p>
 * <p>JDK使用版本号		： jdk6.0 </P>
 * <p>描述				： 原料领用单 Dao </p>
 * @版本					： 1.0.0 
 * @author				： 谢庚才
 *
 * <p>修改历史 </p>
 * <p>修改时间            修改人员    修改内容 </p>
 * <p>Dec 13, 2009       谢庚才     新建 </p>
 */
public class MaterialOutboundSheetDao extends BaseDao {
	private String table = "material_outbound_sheet";
	public MaterialOutboundSheetDao() {
	}
	
	public MaterialOutboundSheetDao(JdbcTemplate jt) {
        this.jt = jt;
    }
	
	public int insert(MaterialOutboundSheetForm materialOutboundSheet) {
		String columns = "mosId, msId, DEPTID, USERID, totalPrice, outboundTime";
        String sql = SqlUtils.getInsertSql(this.table, columns);
        System.out.println(String.format("\n%1$s\n", sql));
        
        if (StringUtils.isBlank(materialOutboundSheet.getMosId())) {
        	materialOutboundSheet.setMosId(UUID.randomUUID().toString());
        }
        
        return this.jt.update(sql,
                new Object[]{
        			materialOutboundSheet.getMosId(),
        			materialOutboundSheet.getMsId(),
        			materialOutboundSheet.getDeptId(),
        			materialOutboundSheet.getUserId(),
        			materialOutboundSheet.getTotalPrice(),
        			materialOutboundSheet.getOutboundTime(),
                },
                new int[]{
        			Types.VARCHAR,
        			Types.VARCHAR,
        			Types.VARCHAR,
        			Types.VARCHAR,
        			Types.DOUBLE,
                    Types.DATE,
                });
	}
	
	public int update(MaterialOutboundSheetForm materialOutboundSheet) {
		String columns = "DEPTID, USERID, totalPrice, outboundTime";
        String sql = SqlUtils.getUpdateSql(this.table, columns, "msiId = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql,
                new Object[]{
        			materialOutboundSheet.getDeptId(),
        			materialOutboundSheet.getUserId(),
        			materialOutboundSheet.getTotalPrice(),
        			materialOutboundSheet.getOutboundTime(),
                },
                new int[]{
        			Types.VARCHAR,
        			Types.VARCHAR,
        			Types.DOUBLE,
                    Types.DATE,
                });
	}
	
	public int delete(String mosId) {
        String sql = SqlUtils.getDeleteSql(this.table, "mosId = ?");
        System.out.println(String.format("\n%1$s\n", sql));
        return this.jt.update(sql, new Object[]{mosId}, new int[]{Types.VARCHAR});
    }
	
	@SuppressWarnings("unchecked")
	public MaterialOutboundSheetForm queryById(String mosId) {
        String sql = "select * from "+this.table+" where mosId = ?";
        List<MaterialOutboundSheetForm> materialOutboundSheetList = this.jt.query(sql,
                new Object[]{mosId}, new int[]{Types.VARCHAR}, new MaterialOutboundSheetMapper());
        if (null != materialOutboundSheetList && 0 < materialOutboundSheetList.size()) {
            return materialOutboundSheetList.get(0);
        }
        return null;
    }
}
